<?php
class ModelDownloadExtraDownloadExtra extends Model {
	public function getDownload($download_extra_id){
		$query = $this->db->query("SELECT v.*, vd.name as name, vd.short_description as short_description, vd.meta_keywords, vd.meta_description, vd.description FROM ".DB_PREFIX."download_extra v LEFT JOIN ".DB_PREFIX."download_extra_description vd ON(vd.download_extra_id=v.download_extra_id) WHERE v.download_extra_id='".(int)$download_extra_id."' AND v.status=1 AND pd.language_id=.".(int)$this->config->get('config_language_id')."'");
		return $query->row;
	}
	public function getCategory(){
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_download_extra c LEFT JOIN " . DB_PREFIX . "category_download_extra_description cd ON (c.category_download_extra_id = cd.category_download_extra_id) WHERE cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC");
		foreach ($query->rows as $result) {
				$category_download_data[] = array(
					'category_download_extra_id' => $result['category_download_extra_id'],
					'name'        => $result['name'],
					'status'  	  => $result['status'],
					'image'		  => $result['image'],
					'sort_order'  => $result['sort_order']
				);
		}
		return $category_download_data;
	}
	public function getDownloadByCategoryDownloadId($category_download_extra_id){
		$query = $this->db->query("SELECT v.*, vd.name as name, vd.short_description as short_description, vd.meta_keywords, vd.meta_description, vd.description FROM ".DB_PREFIX."download_extra v LEFT JOIN ".DB_PREFIX."download_extra_description vd ON(vd.download_extra_id=v.download_extra_id) WHERE v.category_download_extra_id='".(int)$category_download_extra_id."' AND v.status=1 AND pd.language_id=.".(int)$this->config->get('config_language_id')."'");
		return $query->row;
	}
	public function getDownloadById($download_extra_id)
	{
		if($download_extra_id == 0)
		{
			$sql="SELECT v.download_extra_id , v.file_name as file_name, v.date_modify as date_modify,vd.name as name, FROM ".DB_PREFIX."download_extra v , ".DB_PREFIX."download_extra_description vd  WHERE   v.status=1 AND vd.language_id='".(int)$this->config->get('config_language_id')."' ORDER BY v.download_extra_id DESC LIMIT 1";
			
		}else{
			$sql="SELECT v.download_extra_id, ,v.file_name as file_name, v.date_modify as date_modify,vd.name as name, FROM ".DB_PREFIX."download_extra v,".DB_PREFIX."download_extra_description vd WHERE v.download_extra_id='".(int)$download_extra_id."' AND v.status=1 AND vd.language_id='".(int)$this->config->get('config_language_id')."'";
			$this->db->query("UPDATE " . DB_PREFIX . "download_extra SET viewer='". (int)($viewer + 1) . "' WHERE download_extra_id='" . $download_extra_id . "'");
		}
		$query = $this->db->query($sql);
		return $query->row;
	}
	public function getAllDownloadWithoutId()
	{
		$query = $this->db->query("SELECT v.image as image,v.download_extra_id as download_extra_id,v.date_modify as date_modify ,vd.name as name FROM ".DB_PREFIX."download_extra v,".DB_PREFIX."download_extra_description vd WHERE v.download_extra_id=vd.download_extra_id AND v.status=1 AND vd.language_id='".(int)$this->config->get('config_language_id')."' ORDER BY v.download_extra_id DESC LIMIT 8 ");
		return $query->rows;
	}
	
	public function getLastestDownload($limit)
	{
		$query = $this->db->query("SELECT v.image as image,v.download_extra_id as download_extra_id,vd.name as name FROM ".DB_PREFIX."download_extra v,".DB_PREFIX."download_extra_description vd WHERE v.download_extra_id=vd.download_extra_id AND v.status=1 AND vd.language_id='".(int)$this->config->get('config_language_id')."' ORDER BY v.date_modify DESC LIMIT " . $limit );
		return $query->rows;
	}
	public function getNewDownload()
	{
		$sql="SELECT v.*,vd.name as name FROM ".DB_PREFIX."download_extra v,".DB_PREFIX."download_extra_description vd WHERE v.download_extra_id=vd.download_extra_id AND v.status=1 AND vd.language_id='".(int)$this->config->get('config_language_id')."' ORDER BY v.date_modify DESC LIMIT " . 1;
		$query = $this->db->query($sql);
		return $query->row;
	}
	
	public function getDownloadByCatId($cat_id , $data= array() )
	{
		$sql="SELECT v.image as image, v.file_name as file_name,v.download_extra_id as download_extra_id,v.date_modify as date_modify ,vd.name as name,vd.description as description FROM ".DB_PREFIX."download_extra v,".DB_PREFIX."download_extra_description vd WHERE v.download_extra_id=vd.download_extra_id AND v.status=1 AND vd.language_id='".(int)$this->config->get('config_language_id')."' AND v.category_download_extra_id='" . $cat_id . "' ORDER BY  v.download_extra_id DESC ";
		if(isset($data) )
		{
			$sql.= " LIMIT " . $data['start'] . "," . $data['limit'];
		}
		$query = $this->db->query($sql);
		return $query->rows;
	}
	public function getTotalDownloadByCatId($cat_id )
	{
		$sql="SELECT COUNT(*) as total FROM ".DB_PREFIX."download_extra v , ".DB_PREFIX."download_extra_description vd  WHERE vd.download_extra_id=v.download_extra_id AND  v.status=1 AND vd.language_id='".(int)$this->config->get('config_language_id')."' AND v.category_download_extra_id='" . $cat_id . "' ORDER BY v.download_extra_id DESC";
		$query = $this->db->query($sql);
		//print_r($query->row['total']);
		return $query->row['total'];
	}
	
	public function getCategoryDownload($data= array())
	{
		$sql="SELECT c.category_download_extra_id as cat_id, c.image as image, cd.name as name FROM " . DB_PREFIX . "category_download_extra c LEFT JOIN " . DB_PREFIX . "category_download_extra_description cd ON (c.category_download_extra_id = cd.category_download_extra_id) WHERE cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC";
		if(isset($data) )
		{
			$sql.= " LIMIT " . $data['start'] . "," . $data['limit'];
		}
		$query = $this->db->query($sql);
		return $query->rows;
	}
	public function getCategoryByCatId($cat_id)
	{
		$sql="SELECT c.category_download_extra_id as cat_id, c.image as image, cd.name as name, cd.description as description FROM " . DB_PREFIX . "category_download_extra c LEFT JOIN " . DB_PREFIX . "category_download_extra_description cd ON (c.category_download_extra_id = cd.category_download_extra_id) WHERE cd.language_id = '" . (int)$this->config->get('config_language_id') . "'AND c.category_download_extra_id='" . $cat_id . "'";
		$query = $this->db->query($sql);
		return $query->row;
	}
	public function getTotalCategoryDownload()
	{
		$sql="SELECT COUNT(*) as total FROM " . DB_PREFIX . "category_download_extra c LEFT JOIN " . DB_PREFIX . "category_download_extra_description cd ON (c.category_download_extra_id = cd.category_download_extra_id) WHERE cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC";
		$query = $this->db->query($sql);
		return $query->row['total'];
	}
	public function getAllCategoryDownload()
	{
		$sql="SELECT c.category_download_extra_id as cat_id, c.image as image, cd.name as name FROM " . DB_PREFIX . "category_download_extra c LEFT JOIN " . DB_PREFIX . "category_download_extra_description cd ON (c.category_download_extra_id = cd.category_download_extra_id) WHERE cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC";
		$query = $this->db->query($sql);
		return $query->rows;
	}
}
?>